﻿using System.Net.Mail;
using System.Web.Mvc;
using System.Net;

namespace CoVanHocTapProject.Email
{
    public class EmailMVCService
    {
        private readonly EmailMVCTemplateService emailService = new EmailMVCTemplateService();
        private static readonly string viewPath = "~/Views/Emails/{0}.aspx";

        /// <summary>
        /// Takes a view, creates an email from it and sends it out.
        /// </summary>
        /// <param name="context">Context to render the view.</param>
        /// <param name="viewName">View that eventually becomes an email.</param>
        public void SendEmail(ControllerContext context, string viewName, object model, string toMail,string subject) 
        {
            context.Controller.ViewData.Model = model;
            var email = emailService.RenderMessage(context, string.Format(viewPath, viewName), model, toMail, subject);
            email.BodyEncoding = System.Text.Encoding.UTF8;
            SmtpClient client = new SmtpClient();
           
            client.Send(email);
            
            
            //new SmtpClient().Send(email);
        }
    }
}
